package com.hoapital.outpbillwork.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.core.exception.ExceptionHelp;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hoapital.outpbillwork.module.controller.dto.OrderSure;
import com.hoapital.outpbillwork.module.entity.OrderVsBillno;
import com.hoapital.outpbillwork.module.entity.OutpPrepayRcptTemp;
import com.hoapital.outpbillwork.module.mapper.LabTestMasterMapper;
import com.hoapital.outpbillwork.module.mapper.OrderVsBillnoMapper;
import com.hoapital.outpbillwork.module.mapper.OutpPrepayRcptTempMapper;
import com.hoapital.outpbillwork.module.service.OutpPrepayRcptTempService;
import com.hospital.common.core.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

/**
 * <p>
 * ÃÅÕïÔ¤½»½ðÁÙÊ±±í¼ÇÂ¼ 服务实现类
 * </p>
 *
 * @author lvyao
 * @since 2022-01-04
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OutpPrepayRcptTempServiceImpl extends ServiceImpl<OutpPrepayRcptTempMapper, OutpPrepayRcptTemp> implements OutpPrepayRcptTempService {

    @Autowired
    private LabTestMasterMapper labTestMasterMapper;

    @Autowired
    private OutpPrepayRcptTempMapper outpPrepayRcptTempMapper;

    @Autowired
    private OrderVsBillnoMapper orderVsBillnoMapper;

    @Override
    public Map<String, Object> ceateOrderInfo(String patientId, OrderSure orderSure, BigDecimal charges) throws ExceptionHelp {
        Map<String, Object> mapResult = new HashMap<>();
        mapResult.put("code", "0000");
        //查询之前是否有相同的合单数据,所有合单均只按HIS小单号合并时此参数可开否则均不能开启
        OutpPrepayRcptTemp outpPrepayRcptTemp = null;
        String commOrderConfig = outpPrepayRcptTempMapper.getCommOrderConfig();
        if (commOrderConfig != null && commOrderConfig.equals("1")) {
            String billNo = orderSure.getHisBillNo().get(0);
            OrderVsBillno orderVsBillno = orderVsBillnoMapper.selectOne(new QueryWrapper<OrderVsBillno>().apply("bill_no = {0} and rownum = 1", billNo));
            if (!Objects.isNull(orderVsBillno)) {
                String orderNoOne = orderVsBillno.getOrderNo();
                OutpPrepayRcptTemp outpPrepayRcptTempSelect = outpPrepayRcptTempMapper.selectOne(new QueryWrapper<OutpPrepayRcptTemp>().apply("order_no = {0} and SETTLE_FLAG<> 2 and rownum = 1", orderNoOne));
                outpPrepayRcptTemp = outpPrepayRcptTempSelect;
            }
        }
        if (outpPrepayRcptTemp == null) {
            outpPrepayRcptTemp = new OutpPrepayRcptTemp();
            //组装交易订单记录
            outpPrepayRcptTemp.setPatientId(patientId);
            String orderNumber = UUID.randomUUID().toString().replace("-", "");
            String guid = labTestMasterMapper.getGuid();
//        BigDecimal allapplyCharges = new BigDecimal("0");
            if (orderSure.getOutpOrInp() == null || orderSure.getOutpOrInp().trim().equals("") || orderSure.getSettleFlag() == null || orderSure.getSettleFlag().trim().equals("")) {
                mapResult.put("code", String.valueOf(ExceptionHelpEnum.ORDER_COMM.getCode()));
                mapResult.put("message", ExceptionHelpEnum.ORDER_COMM.getMessage());
                return mapResult;
            }
            outpPrepayRcptTemp.setGuid(guid);
            outpPrepayRcptTemp.setOrderNo(orderNumber);
            outpPrepayRcptTemp.setReturnAmount(new BigDecimal("0"));
            String CompanyFlag = orderSure.getCreateChannel().equals("1") ? "微信" : orderSure.getCreateChannel().equals("2") ? "柜面" : orderSure.getCreateChannel().equals("3") ? "自助机" : "支付宝";
            outpPrepayRcptTemp.setTransactType("结算");
            outpPrepayRcptTemp.setSettleFlag(orderSure.getSettleFlag());
            outpPrepayRcptTemp.setHisTransactDate(Timestamp.valueOf(DateUtil.getTime()));
            outpPrepayRcptTemp.setHisTranStatus("1");
            outpPrepayRcptTemp.setPayWay(CompanyFlag);

            outpPrepayRcptTemp.setInpOrOutp(String.valueOf(Integer.parseInt(orderSure.getOutpOrInp())));
            outpPrepayRcptTemp.setAmount(charges);
            try {
                Integer insert = outpPrepayRcptTempMapper.insert(outpPrepayRcptTemp);
                if (insert == 0) {
                    throw new ExceptionHelp(ExceptionHelpEnum.INSERT_ORDER_INFO);
                }
                for (String billNo : orderSure.getHisBillNo()) {
                    OrderVsBillno orderVsBillno = new OrderVsBillno();
                    orderVsBillno.setOrderNo(orderNumber);
                    orderVsBillno.setBillNo(billNo);
                    Integer insert1 = orderVsBillnoMapper.insert(orderVsBillno);
                    if (insert1 == 0) {
                        throw new ExceptionHelp(ExceptionHelpEnum.INSERT_ORDER_DETAIL);
                    }
                }
            } catch (Exception e) {
                throw new ExceptionHelp(ExceptionHelpEnum.INSERT_ORDER_INFO_1);
            }
        }
        mapResult.put("outpPrepayRcptTemp", outpPrepayRcptTemp);
        return mapResult;
    }

    @Override
    public boolean updateFlag(String patientId, String orderNO) {

        return outpPrepayRcptTempMapper.updateFlag(patientId, orderNO);
    }

    @Override
    public List<OutpPrepayRcptTemp> getSingleBill(String billNo) {
        return outpPrepayRcptTempMapper.getSingleBill(billNo);
    }

    @Override
    public String getCommOrderConfig() {
        return outpPrepayRcptTempMapper.getCommOrderConfig();
    }
}
